package hu.vpszd.services.impl

import hu.vpszd.entities.transaction.LoanApplication
import hu.vpszd.services.CalculationService
import hu.vpszd.services.MinimumConditionCheckingService

import org.springframework.stereotype.Service

@Service
class CalculationServiceImpl implements CalculationService {

	final static Double LOANTYPE_1_INTEREST = 1.1
	final static int LOANTYPE_1_MAXDURATION = 80

	final static Double LOANTYPE_2_INTEREST = 1.15
	final static int LOANTYPE_2_MAXDURATION = 100

	final static int LOANTYPE_1_MAXDAMOINT = 2000000


	public LoanApplication calculation(LoanApplication loanApplication){

		int totalCountableIncomeAmount = 0
		if (loanApplication.status == 0 || loanApplication.status == 1){
			for(i in loanApplication.incomes){
				totalCountableIncomeAmount += i.incomeAmount
			}
			if (loanApplication.loanType == 1){
				int n1 = (loanApplication.loanAmountByCustomer*LOANTYPE_1_INTEREST)/loanApplication.durationByCustomer
				int n2 = totalCountableIncomeAmount-50000
				int n3 = (loanApplication.loanAmountByCustomer/n2)+6
				if(n3 > 84){
					n1 = loanApplication.loanAmountByCustomer/(loanApplication.durationByCustomer*2)
					n3 = loanApplication.loanAmountByCustomer/n2
					if(n3>84){
						loanApplication.loanAmountByBank = 0
						loanApplication.durationByBank = 0
						loanApplication.installment = 0
						loanApplication.interest = 0
						loanApplication.isOfferApproved = false
					}
				}
				else{
					loanApplication.loanAmountByBank = loanApplication.loanAmountByCustomer
					loanApplication.durationByBank = n3
					loanApplication.installment = (loanApplication.loanAmountByBank/n3)*1.3
					loanApplication.interest = LOANTYPE_1_INTEREST
					loanApplication.isOfferApproved = true
				}

			}
			if (loanApplication.loanType == 2){

				int n1 = (loanApplication.loanAmountByCustomer*LOANTYPE_1_INTEREST)/loanApplication.durationByCustomer
				int n2 = totalCountableIncomeAmount-50000
				int n3 = (loanApplication.loanAmountByCustomer/n2)+6
				if(n3 > 120 ){
					n1 = loanApplication.loanAmountByCustomer/(loanApplication.durationByCustomer*2)
					n3 = loanApplication.loanAmountByCustomer/n2
					if(n3>120){
						loanApplication.loanAmountByBank = 0
						loanApplication.durationByBank = 0
						loanApplication.installment = 0
						loanApplication.interest = 0
						loanApplication.isOfferApproved = false
					}
				}
				else{
					loanApplication.loanAmountByBank = loanApplication.loanAmountByCustomer
					loanApplication.durationByBank = n3
					loanApplication.installment = (loanApplication.loanAmountByBank/n3)*1.3
					loanApplication.interest = LOANTYPE_1_INTEREST
					loanApplication.isOfferApproved = true
				}
			}
			loanApplication.isCalculated = true

			if(loanApplication.loanAmountByBank != 0 && loanApplication.durationByBank != 0 && loanApplication.installment != 0 && loanApplication.isMinimumConditionApproved){
				if (loanApplication.status == 0 || loanApplication.status == 1){
					loanApplication.isOfferApproved = true
				}
				else{
					loanApplication.isOfferApproved = false
					println("isOfferApproved: " + loanApplication.isOfferApproved)
				}
				if(loanApplication.status == 1){
					loanApplication.isApplicationApproved = true
				}
				else{
					loanApplication.isApplicationApproved = false
				}
			}
			else{}


		}
		//Bírálat: ez abban különbözik, h a verifeket is kiértékeli
		else if (loanApplication.status == 2){

		}
		else {
			//HIBA
		}

		return loanApplication
	}
}
